package com.salts.controller;

import com.salts.domain.dto.*;
import com.salts.domain.po.OrderInfo;
import com.salts.domain.po.UserInfo;
import com.salts.domain.vo.OrderVo;
import com.salts.domain.vo.UserVo;
import com.salts.service.MineService;
import org.apache.http.HttpEntity;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.io.IOException;
import java.lang.reflect.InvocationTargetException;
import java.util.*;
import java.util.stream.Collectors;

/**
 * "我的"模块接口  /mine
 */
@RestController
@RequestMapping("/mine")
public class MineController {
    @Autowired
    MineService mineService;

    /**
     * 用户信息获取接口 /getUserInfo -Post
     */
    @PostMapping("/getUserInfo")
    public Result getUserInfo(@RequestBody QueryUserInfoDto queryUserInfoDto, UserInfo user){
     Integer userId = queryUserInfoDto.getUserId();
     user.setId(userId);
        UserVo userInfo = mineService.getUserInfo(user);
        return Result.ok(userInfo);
    }//用户信息获取接口

    /**
     * 用户订单列表数量接口 /getOrderCount -Post
     */
    @PostMapping("/getOrderCount")
    public Result getOrderCount(@RequestBody Map<String,Integer> paramMap,UserInfo user){
        user.setId(paramMap.get("userId"));
        List<OrderStateDto> orderCount = null;
        try {
            orderCount = mineService.getOrderCount(user);
            List<String> names = orderCount.stream().map(r -> r.getOrderStatusName()).collect(Collectors.toList());
            if (!names.contains("待付款")){
                OrderStateDto orderStateDto = new OrderStateDto();
                orderStateDto.setOrderStatusName("待付款");
                orderStateDto.setOrderState(Arrays.asList(0));
                orderStateDto.setOrderCount(0);
                orderCount.add(orderStateDto);
            }if (!names.contains("待配送")){
                OrderStateDto orderStateDto = new OrderStateDto();
                orderStateDto.setOrderStatusName("待配送");
                orderStateDto.setOrderState(Arrays.asList(1,2,3,4));
                orderStateDto.setOrderCount(0);
                orderCount.add(orderStateDto);
            }if (!names.contains("待收货")){
                OrderStateDto orderStateDto = new OrderStateDto();
                orderStateDto.setOrderStatusName("待收货");
                orderStateDto.setOrderState(Arrays.asList(5));
                orderStateDto.setOrderCount(0);
                orderCount.add(orderStateDto);
            }if (!names.contains("退款/售后")){
                OrderStateDto orderStateDto = new OrderStateDto();
                orderStateDto.setOrderStatusName("退款/售后");
                orderStateDto.setOrderState(Arrays.asList(-1));
                orderStateDto.setOrderCount(0);
                orderCount.add(orderStateDto);
            }
        } catch (InvocationTargetException | IllegalAccessException e) {
            throw new RuntimeException(e);
        }
        return Result.ok(orderCount);
    }//用户订单列表数量接口

    /**
     * 订单详情接口 /getOrderInfo -Post
     */
    @PostMapping("/getOrderInfo")
    public Result getOrderInfo(@RequestBody GetOrderInfoDto param, UserInfo user) {
        List<OrderVo> orderInfo = mineService.getOrderInfo(param);
        return Result.ok(orderInfo);
    }//订单详情接口
    //优惠券接口暂时不做

    /**
     * 反馈信息接口 /feedback -Post
     * @return Result。getData()
     * @return 固定返回值：{"qq":"2072698911","微信":"13811204074","技术负责人":"喵吉"}
     */
    //账户余额接口暂时不做
    @RequestMapping("/feedback")
    public String feedback(){
        String feedback = mineService.feedback();
        return feedback;
    }//意见反馈接口

    /**
     * 修改个人信息接口
     * @return
     */
    @RequestMapping("/updateUserInfo")
    public Result updateUserInfo(@RequestBody UserInfoDto userDto){
        UserInfo userInfo = new UserInfo();
        userInfo.setId(userDto.getUserId());
        userInfo.setAvatar(userDto.getAvatar());
        userInfo.setEmail(userDto.getEmail());
        userInfo.setGender(userDto.getGender());
        userInfo.setMobile(userDto.getMobile());
        userInfo.setUserName(userDto.getUserName());
        Result feedback = mineService.updateUserInfo(userInfo);
        return feedback;
    }//修改个人信息接口

    /**
     * 订单物流单号查询接口
     * @param orderInfo
     * @return
     */
    @RequestMapping("/logistics")
    public Result queryLogistics(@RequestBody OrderInfo orderInfo){
         Integer orderState = orderInfo.getOrderState();
        LogisticsDto returnInfo= null;
        CloseableHttpClient client = null;
        CloseableHttpResponse response = null;
        String jsonRequest=null;
        try {
            returnInfo = mineService.queryLogistics(orderInfo);//获取单号查询物流
            if (!returnInfo.getLogisticsInfomation().isEmpty()){
                /*String YTOURL="https://www.yto.net.cn/ec/order/gwWaybillInfoList";
                if (returnInfo.getLogisticsCompany().equals("YTO")){
                    String logisticsInfomation = returnInfo.getLogisticsInfomation();//拿到单号
                    jsonRequest="[\""+logisticsInfomation+"\"]";
                }
                // 创建http Post请求
                HttpPost httpPost = new HttpPost(YTOURL);
                // 设置请求体
                StringEntity requestEntity = new StringEntity(jsonRequest);
                httpPost.setEntity(requestEntity);
                client = HttpClients.createDefault();
                // 执行请求
                response = client.execute(httpPost);
                HttpEntity entity = response.getEntity();//得到返回数据
                String result = EntityUtils.toString(entity);*/
                return Result.ok(returnInfo);
            }
            return Result.fail("该订单暂时没有物流信息");
        } catch (InvocationTargetException e) {
            throw new RuntimeException(e);
        } catch (IllegalAccessException e) {
            throw new RuntimeException(e);
        }
    }

}
